/*
 * Copyright (C) 2011 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.aergia.vinny.util.IconUtil;

/**
 * @author Fred
 */
public class DatabaseActionPanel extends JPanel {

	public static final String ACTION_ADD = "ADD";

	public static final String ACTION_DELETE = "DELETE";
	public static final String ACTION_EDIT = "EDIT";
	/**
	 * 
	 */
	private static final long serialVersionUID = -2512316762458218684L;

	private JButton addButton;

	private JButton deleteButton;
	private JButton editButton;

	public DatabaseActionPanel(final ActionListener listener) {
		this(listener, true, true, null);
	}

	public DatabaseActionPanel(final ActionListener listener, final boolean includeOperations, final boolean includeModify, final JPanel additionalPanel) {
		this.initComponents(listener, includeOperations, includeModify, additionalPanel);
	}

	private JPanel addButton(final JButton button) {
		final JPanel panel = new JPanel();
		panel.add(button);
		return panel;
	}

	private void initComponents(final ActionListener listener, final boolean includeOperations, final boolean includeModify, final JPanel additionalPanel) {
		this.setLayout(new BorderLayout());

		final JPanel buttonPanel = new JPanel(new GridLayout(1, 3));
		if (includeOperations) {
			this.addButton = new JButton();
			this.addButton.setText("Add");
			this.addButton.setIcon(IconUtil.getIcon("/images/32/add.png"));
			this.addButton.setActionCommand(DatabaseActionPanel.ACTION_ADD);
			this.addButton.addActionListener(listener);
			buttonPanel.add(this.addButton(this.addButton));

			this.deleteButton = new JButton();
			this.deleteButton.setText("Disable");
			this.deleteButton.setIcon(IconUtil.getIcon("/images/32/delete.png"));
			this.deleteButton.setActionCommand(DatabaseActionPanel.ACTION_DELETE);
			this.deleteButton.addActionListener(listener);
			buttonPanel.add(this.addButton(this.deleteButton));
			if (!includeModify) {
				buttonPanel.add(new JLabel());
			}
		}

		if (includeModify) {
			this.editButton = new JButton();
			this.editButton.setText("Edit");
			this.editButton.setIcon(IconUtil.getIcon("/images/32/edit.png"));
			this.editButton.setActionCommand(DatabaseActionPanel.ACTION_EDIT);
			this.editButton.addActionListener(listener);
			buttonPanel.add(this.addButton(this.editButton));
			if (!includeOperations) {
				buttonPanel.add(new JLabel());
				buttonPanel.add(new JLabel());
			}
		}

		this.add(buttonPanel, BorderLayout.WEST);
		this.add(additionalPanel != null ? additionalPanel : new JLabel(), BorderLayout.EAST);
	}

}
